package cn.lyjuan.payment.third.base.pay.chl.impl.ali.util;

import cn.lyjuan.payment.third.base.pay.chl.impl.ali.bean.AliChlCnf;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝退款
 * todo 修改
 *
 * @author hhw
 */
public class AlipayRefund {
//    public static <T> T createTradeExec(String localNo, Integer totalAmt, Integer amt, AliChlCnf cnf, boolean isDebug) {
//        return null;
//    }

    public static AlipayTradeRefundResponse createTradeExec(String appRefundNo, String localNo, Integer amt, AliChlCnf cnf, boolean isDebug) {
        DefaultAlipayClient alipayClient;
        try {
            alipayClient = AlipaySubmit.makeCertAlipayClient(cnf.getCertPath(), cnf, isDebug);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
//            String out_request_no = RandomUtils.uuid();
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            Map<String, String> map = new HashMap<>();
            map.put("out_trade_no", localNo);
            map.put("refund_amount", Double.toString(amt / 100.0));
//            if (totalAmt != amt)
            map.put("out_request_no", appRefundNo);
            request.setBizContent(JSONObject.toJSONString(map));
            //            if (totalAmt != amt)
//                response.setTradeNo(appRefundNo);
            return alipayClient.certificateExecute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }


}
